// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
// 通过openid获取喜欢列表
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const _ = db.command
  let likeRes = await db.collection("like").where({
    openid:wxContext.OPENID
  }).get();
  let collectRes = await db.collection("collect").where({
    openid:wxContext.OPENID
  }).get();
  const like = likeRes.data;
  let collect = collectRes.data;
  let goodsIds = like.map(el=>el.classifyId);
  let collectC = collect.map(el=>el.goodsId);
  // let goodsLikeRes = await db.collection("goods").where({
  //   _id:_.in(goodsIds)
  // }).get();

  let goodsCollectRes = await db.collection("goods").where({
    _id:_.in(collectC)
  }).get();

  let orderDRes = await db.collection("order").where({
    openid:wxContext.OPENID,
    status:1
  }).get();

  let orderCRes = await db.collection("order").where({
    openid:wxContext.OPENID,
    status:2
  }).get();






  return {
    likes:like,
    collects:goodsCollectRes.data,
    orderD:orderDRes.data,
    orderC:orderCRes.data,
    collectRes,
    likeRes,
    collectC,
    goodsIds
  }
}